<%@page import="net.groupme.user.UserManager"%>
<%@page import="java.util.List"%>
<%@page import="net.groupme.model.DashboardAnnouncement"%>
<%@page import="java.util.Iterator"%>
<%@page import="net.groupme.model.*"%>
<%@page import="net.groupme.model.ObjectFactory"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="net.groupme.gui.constants.VIEW_VOCABULARY"%>
<%@page import="net.groupme.model.dao.DATABASE_CONSTANTS"%>
<%@page import="net.groupme.model.MODEL_CONSTANTS"%>
<div class="grouplistHead" style="margin-top:1em;">
	<div class="groupListHeadMenu">
	</div>
	<h4>Your Dashboard
		<span style="font-size: x-small; color: gray; ">
		<% 
		UserManager um = (UserManager) WebApplicationContextUtils.getWebApplicationContext(pageContext.getServletContext())
		.getBean(VIEW_VOCABULARY.USER_MANGER, UserManager.class);
		%>
		</span> 
	</h4>
</div>
<div id="mainGroupwindowContent" class="groupWithinGroupList" style="font-size: 0.8em; margin-top: 1em;">
	<% 	List<DashboardAnnouncement> ann = um.getLatest5DashboardAnnouncements();%>
				<% 	Iterator<DashboardAnnouncement> it = ann.iterator(); %>
				<% 	DashboardAnnouncement da = new DashboardAnnouncement();
					if(!it.hasNext()){%>
					- No News for you -
				<% 	}while (it.hasNext()) { 
						da = it.next(); %>
						
						<% if (DATABASE_CONSTANTS.TABLE_LOG_EVENT_ACTION_GROUP_CREATED.equals(da.getAction())){%>
						
						
						<% 
						//try to load a group image
						String thumbnail = null;
						String defaultgroupThumbnail = request.getContextPath() + "/images/groupimg-default.gif";
						String groupID = String.valueOf(da.getGroupId());
						Group groupItem = ObjectFactory.getGroup(groupID);
						
						Iterator<User> userIter = groupItem.getUsers("owner").iterator();
						User groupOwner = null;
						if (userIter.hasNext()){
							groupOwner = userIter.next();
						}
						
						try{
							thumbnail = groupItem.getActualGroupVisualization(groupOwner.getId()).getThumbnail();
						}
						catch(Exception e){
							
						}
						
						
			    		if (thumbnail== null){
							thumbnail = groupItem.getURLOfAPictureInGroup();
						}
								
						if (thumbnail== null & groupItem.getURLOfAPictureInGroup()==null){
							thumbnail = defaultgroupThumbnail;
						}
						
						%>
						
						<div class="resultListItem">
							<div style="float:left; margin-right:1em; height: 60px;">
								<img src="<%=thumbnail %>" height="45px" width="75px" onerror="this.src='<%=defaultgroupThumbnail %>';"/>
							</div>
							<strong>A new Group was created</strong><br/>
							<p> <span style="font-size:0.8em;color:gray"><%= da.getDate().substring(0,19) %></span></p>
						</div>
						
						<%} else if (DATABASE_CONSTANTS.TABLE_LOG_EVENT_ACTION_GROUP_DELETED.equals(da.getAction())){
						
						String thumbnail = request.getContextPath() + "/images/delete-48x48.gif";
						%>
						
						<div class="resultListItem">
							<div style="float:left; margin-right:1em; height: 60px;">
								<img src="<%=thumbnail %>" height="45px" />
							</div>
							<strong>Group deleted</strong><br/>
							<p> <span style="font-size:0.8em;color:gray"><%= da.getDate().substring(0,19) %></span></p>
						</div>
						
						<%} else if (DATABASE_CONSTANTS.TABLE_LOG_EVENT_ACTION_GROUP_SUBSCRIBED.equals(da.getAction())){
						
							String thumbnail = null;
							String defaultgroupThumbnail = request.getContextPath() + "/images/groupimg-default.gif";
							String groupID = String.valueOf(da.getGroupId());
							Group groupItem = ObjectFactory.getGroup(groupID);
							
							Iterator<User> userIter = groupItem.getUsers("owner").iterator();
							User groupOwner = null;
							if (userIter.hasNext()){
								groupOwner = userIter.next();
							}
							
							thumbnail = groupItem.getActualGroupVisualization(groupOwner.getId()).getThumbnail();
				    		if (thumbnail== null){
								thumbnail = groupItem.getURLOfAPictureInGroup();
							}
									
							if (thumbnail== null & groupItem.getURLOfAPictureInGroup()==null){
								thumbnail = defaultgroupThumbnail;
							}	
						%>
						
						<div class="resultListItem">
							<div style="float:left; margin-right:1em; height: 60px;">
								<img src="<%=thumbnail %>" height="45px" width="75px" onerror="this.src='<%=defaultgroupThumbnail %>';"/>
							</div>
							<strong>A Group was subscribed</strong><br/>
							<a href="./<%=VIEW_VOCABULARY.REQUEST_TYPE_USER%>/<%=da.getUserId()%>">User</a> subscribed to your 
							Group: <a href="./<%=VIEW_VOCABULARY.REQUEST_TYPE_GROUP %>/<%=da.getGroupId()%>"><%=da.getGroupName()%></a>.
							<p> <span style="font-size:0.8em;color:gray"><%= da.getDate().substring(0,19) %></span></p>
						</div>

						<%} else if (DATABASE_CONSTANTS.TABLE_LOG_EVENT_ACTION_GROUP_UNSUBSCRIBED.equals(da.getAction())){
							String thumbnail = null;
							String defaultgroupThumbnail = request.getContextPath() + "/images/groupimg-default.gif";
							String groupID = String.valueOf(da.getGroupId());
							Group groupItem = ObjectFactory.getGroup(groupID);
							
							Iterator<User> userIter = groupItem.getUsers("owner").iterator();
							User groupOwner = null;
							if (userIter.hasNext()){
								groupOwner = userIter.next();
							}
							
							thumbnail = groupItem.getActualGroupVisualization(groupOwner.getId()).getThumbnail();
				    		if (thumbnail== null){
								thumbnail = groupItem.getURLOfAPictureInGroup();
							}
									
							if (thumbnail== null & groupItem.getURLOfAPictureInGroup()==null){
								thumbnail = defaultgroupThumbnail;
							}

						%>
						
						<div class="resultListItem">
							<div style="float:left; margin-right:1em; height: 60px;">
								<img src="<%=thumbnail %>" height="45px" width="75px" onerror="this.src='<%=defaultgroupThumbnail %>';"/>
							</div>
							<strong>A Group was unsubscribed</strong><br/>
							<a href="./<%=VIEW_VOCABULARY.REQUEST_TYPE_USER%>/<%=da.getUserId()%>">User</a> unsubscribed your
							Group: <a href="./<%=VIEW_VOCABULARY.REQUEST_TYPE_GROUP %>/<%=da.getGroupId()%>"><%=da.getGroupName()%></a>.
							<p> <span style="font-size:0.8em;color:gray"><%= da.getDate().substring(0,19) %></span></p>
						</div>

						<%} else if (DATABASE_CONSTANTS.TABLE_LOG_EVENT_ACTION_GROUP_TAGGED.equals(da.getAction())){
							String thumbnail = null;
							String defaultgroupThumbnail = request.getContextPath() + "/images/groupimg-default.gif";
							String groupID = String.valueOf(da.getGroupId());
							Group groupItem = ObjectFactory.getGroup(groupID);
							
							Iterator<User> userIter = groupItem.getUsers("owner").iterator();
							User groupOwner = null;
							if (userIter.hasNext()){
								groupOwner = userIter.next();
							}
							
							thumbnail = groupItem.getActualGroupVisualization(groupOwner.getId()).getThumbnail();
				    		if (thumbnail== null){
								thumbnail = groupItem.getURLOfAPictureInGroup();
							}
									
							if (thumbnail== null & groupItem.getURLOfAPictureInGroup()==null){
								thumbnail = defaultgroupThumbnail;
							}
						
						%>
						<div class="resultListItem">
							<div style="float:left; margin-right:1em; height: 60px;">
								<img src="<%=thumbnail %>" height="45px" width="75px"  onerror="this.src='<%=defaultgroupThumbnail %>';"/>
							</div>
							<strong>A Group was tagged</strong><br/>
							Your <%if (MODEL_CONSTANTS.ROLE_SUBSCRIBER.equals(da.getRole())){ %> subscribed <%} %> 
							Group: <a href="./<%=VIEW_VOCABULARY.REQUEST_TYPE_GROUP %>/<%=da.getGroupId()%>"><%=da.getGroupName()%></a>
							was tagged with <a href=" <%=VIEW_VOCABULARY.GROUPME_BASE_URL%><%=VIEW_VOCABULARY.ACTION_HOME%>?<%=VIEW_VOCABULARY.PARAMETER_MYHOME_VIEW%>
							=<%=VIEW_VOCABULARY.VIEW_SEARCH_RESULT%>&<%=VIEW_VOCABULARY.PARAMETER_KEYWORD%>=<%=da.getTag()%>"
							title="Search for your Groups and Resources tagged with <%=da.getTag()%>"><%=da.getTag()%></a>.
							<p> <span style="font-size:0.8em;color:gray"><%= da.getDate().substring(0,19) %></span></p>
						</div>
						
						
						
						
						
						<%} else if (DATABASE_CONSTANTS.TABLE_LOG_EVENT_ACTION_RESOURCE_CREATED.equals(da.getAction())){
							String thumbnail = null;
							String defaultgroupThumbnail = request.getContextPath() + "/images/groupimg-default.gif";
							String groupID = String.valueOf(da.getGroupId());
							Group groupItem = ObjectFactory.getGroup(groupID);
							
							Iterator<User> userIter = groupItem.getUsers("owner").iterator();
							User groupOwner = null;
							if (userIter.hasNext()){
								groupOwner = userIter.next();
							}
							
							thumbnail = groupItem.getActualGroupVisualization(groupOwner.getId()).getThumbnail();
				    		if (thumbnail== null){
								thumbnail = groupItem.getURLOfAPictureInGroup();
							}
									
							if (thumbnail== null & groupItem.getURLOfAPictureInGroup()==null){
								thumbnail = defaultgroupThumbnail;
							}
						
						%>
						<div class="resultListItem">
							<div style="float:left; margin-right:1em; height: 60px;">
								<img src="<%=thumbnail %>" height="45px" width="75px" onerror="this.src='<%=defaultgroupThumbnail %>';"/>
							</div>
							<strong>A new Resource was added</strong><br/>
							A new Resource: <a href="<%= da.getResourceURI()%>"><%=da.getResourceName()%></a> was added to your subscribed
							Group: <a href="./<%=VIEW_VOCABULARY.REQUEST_TYPE_GROUP %>/<%=da.getGroupId()%>"><%=da.getGroupName()%></a>.
							<p> <span style="font-size:0.8em;color:gray"><%= da.getDate().substring(0,19) %></span></p>
						</div>
						
						
						
						<%} else if (DATABASE_CONSTANTS.TABLE_LOG_EVENT_ACTION_RESOURCE_REMOVED.equals(da.getAction())){
							String thumbnail = null;
							String defaultgroupThumbnail = request.getContextPath() + "/images/groupimg-default.gif";
							String groupID = String.valueOf(da.getGroupId());
							Group groupItem = ObjectFactory.getGroup(groupID);
							
							Iterator<User> userIter = groupItem.getUsers("owner").iterator();
							User groupOwner = null;
							if (userIter.hasNext()){
								groupOwner = userIter.next();
							}
							
							thumbnail = groupItem.getActualGroupVisualization(groupOwner.getId()).getThumbnail();
				    		if (thumbnail== null){
								thumbnail = groupItem.getURLOfAPictureInGroup();
							}
									
							if (thumbnail== null & groupItem.getURLOfAPictureInGroup()==null){
								thumbnail = defaultgroupThumbnail;
							}
						%>
						
						<div class="resultListItem">
							<div style="float:left; margin-right:1em; height: 60px;">
								<img src="<%=thumbnail %>" height="45px" width="75px" onerror="this.src='<%=defaultgroupThumbnail %>';"/>
							</div>
							<strong>Resource was removed</strong><br/>
							The Resource: <a href="<%=  da.getResourceURI()%>"><%=da.getResourceName()%></a> was removed from your subscribed
							Group: <a href="./<%=VIEW_VOCABULARY.REQUEST_TYPE_GROUP %>/<%=da.getGroupId()%>"><%=da.getGroupName()%></a>.
							<p> <span style="font-size:0.8em;color:gray"><%= da.getDate().substring(0,19) %></span></p>
						</div>
						
						
						
						<%} else if (DATABASE_CONSTANTS.TABLE_LOG_EVENT_ACTION_RESOURCE_TAGGED.equals(da.getAction())){
							String thumbnail = null;
							String defaultgroupThumbnail = request.getContextPath() + "/images/groupimg-default.gif";
							String groupID = String.valueOf(da.getGroupId());
							Group groupItem = ObjectFactory.getGroup(groupID);
							
							Iterator<User> userIter = groupItem.getUsers("owner").iterator();
							User groupOwner = null;
							if (userIter.hasNext()){
								groupOwner = userIter.next();
							}
							
							thumbnail = groupItem.getActualGroupVisualization(groupOwner.getId()).getThumbnail();
				    		if (thumbnail== null){
								thumbnail = groupItem.getURLOfAPictureInGroup();
							}
									
							if (thumbnail== null & groupItem.getURLOfAPictureInGroup()==null){
								thumbnail = defaultgroupThumbnail;
							}
						%>
						<div class="resultListItem">
							<div style="float:left; margin-right:1em; height: 60px;">
								<img src="<%=thumbnail %>" height="45px" width="75px" onerror="this.src='<%=defaultgroupThumbnail %>';"/>
							</div>
							<strong>Resource was tagged</strong><br/>
							The Resource: <a href="<%=  da.getResourceURI()%>"><%=da.getResourceName()%></a> in your 
						
							<%if (MODEL_CONSTANTS.ROLE_SUBSCRIBER.equals(da.getRole())){ %> subscribed <%} %> Group: <a href="./<%=VIEW_VOCABULARY.REQUEST_TYPE_GROUP %>/<%=da.getGroupId()%>"><%=da.getGroupName()%></a>
							was tagged with <a href=" <%=VIEW_VOCABULARY.GROUPME_BASE_URL%><%=VIEW_VOCABULARY.ACTION_HOME%>?<%=VIEW_VOCABULARY.PARAMETER_MYHOME_VIEW%>
							=<%=VIEW_VOCABULARY.VIEW_SEARCH_RESULT%>&<%=VIEW_VOCABULARY.PARAMETER_KEYWORD%>=<%=da.getTag()%>"
							title="Search for your Groups and Resources tagged with <%=da.getTag()%>"><%=da.getTag()%></a>.
							<p> <span style="font-size:0.8em;color:gray"><%= da.getDate().substring(0,19) %></span></p>
						</div>
						<%} 
						if(it.hasNext()) {%><%} %>
				<%}  %>
</div>
